<?php

class Application_Model_UserAccountHistory extends Application_Model_DomainObject{
    
    /**
     * 扣钱 
     */
    const TYPE_DISMONEY=1;
    
    /**
     * 充值 
     */
    const TYPE_RECHARGE_MONEY=2;
    
    /**
     * 扣分 
     */
    const TYPE_DISSCORE=3;
    
    /**
     * 手工 
     */
    const TYPE_MANU=0;

    protected $_id;
    
    /**
     * 1、扣除余额，2、充值,3、扣除积分,0,手工
     * @var type 
     */
    protected $_type;
    
    protected $_beforeMoney;
    
    protected $_afterMoney;
    
    protected $_comment;
    
    protected $_dateCreated;
    
    protected $_relaseId;
    
    protected $_beforeScore;
    
    protected $_afterScore;
    
    protected $_accountId;
    
    protected $_state;
    
    protected $_source;


    private static function getDb(){
        return new Application_Model_DbTable_UserAccountHistory();
    }
    
    /**
     * 查询历史记录
     * @param int $userId 用户ID
     * @param int $type 操作类型
     * @param date $dateStart 开始时间
     * @param date $dateEnd 结速时间
     * @param int $count 每页记录数
     * @param int $offset 从第几条开始
     * @return arrry 返回格式为 array('size'=>?,'list'=>?,'pages'=>?,'index'=>?) 
     * size为查询记录的总条数，list为返回的array型数据，pages是指有多少页，index为当前是哪一页
     */
    public static function search($userId,$type,$dateStart,$dateEnd){
        return Application_Model_UserAccountHistory::getDb()->search($userId,$type,$dateStart,$dateEnd,20,0);
    }
    
    public static function getMoneyChange($userId,$dateStart,$dateEnd, $count, $offset){

    	return Application_Model_UserAccountHistory::getDb()->queryPage(array('accountId'=>$userId,' and dateCreated >='=>$dateStart,' and dateCreated <='=>$dateEnd),'dateCreated desc',$count, $offset);
    }

	/**
     * 返回充值了多少金额
     * @author wuzw
     */
    public function getWidthDrawMoney()
    {
    	return $this->_afterMoney - $this->_beforeMoney;
    }
	
    /**
     * 返回使用了多少金额
     * @return type 
     */
    public function getUseMoney(){
        return $this->_beforeMoney - $this->_afterMoney;
    }
    
    /**
     * 返回使用了多少积分
     * @return type 
     */
    public function getUseScore(){
        return $this->_beforeScore - $this->_afterScore;
    }
    public static function getMyFilter($qucik,$startTime,$endTime,$count, $offset){
        $where = array();
        $and = '';
        if($qucik != NULL){
            $uwhere[" nikename like "] = "%".$qucik."%";
            $uwhere[" or storeno = "] = $qucik;
            $array_user = Application_Model_User::getDao()->queryAll($uwhere);
            if($array_user){
                $userid = "(";
                foreach ($array_user as $uvalue) {
                    $userid .= $uvalue->id.",";
                }
                $userid .= "0)";
                $where['accountId in '] = $userid; 
                $and = ' and ';
            }else{
                return null;
            }
        }
        if ($startTime != NULL) {
            $where[$and . ' dateCreated >= '] = $startTime;
            $and = ' and ';
        }

        if ($endTime != NULL) {
            $where[$and . ' dateCreated <= '] = $endTime;
            $and = ' and ';
        }
        return Application_Model_UserAccountHistory::getDb()->queryPage($where, "  id desc ", $count, $offset);
    }
     public function gettypename()
    {        
        //return $this->_type;
        $result = Application_Model_UserAccountHistory::getStatusEnum();
        return $result[$this->_type];
    }
    public static function getStatusEnum(){
        return array(0=>'手工',1=>'扣除余额',2=>'充值',3=>'扣除积分',4=>'退款');
    }
    public function getusername()
    {        
        if($this->_accountId)
        {
            return Application_Model_User::load($this->_accountId)->nikename;
        }
        return;
    }
    public function getbystoreno()
    {
        return Application_Model_User::getByUserId($this->_accountId);
    }
}